<?

//Data loading
$title='REPORT DEBIT NOTE OUTSTANDING';
$curr = GetParam("curr","");

$perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';
$TableName = "tbljoactivity";
$DEFINE_FONT=0;
$pdf=new PDFReport("L","mm","Letter");
$pdf->AddPage();
$Result = ReportList ("", "", "");
if ($Result) $pdf->ReportTable($Result);
$pdf->Output();



function ReportList ($Space, $Index, $ParentID) {
            global $RS,$Index,$TableName,$curr;
            $DBConnection   = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
            if (!$Index) $Index = 0;

$SQL = " SELECT a.*,b.*,c.*,d.CUNAME AS bill_debtur,e.CUNAME AS cust_debtur FROM `$TableName` as a
         LEFT JOIN tbljoborder as b ON a.jo_id = b.jo_id
         LEFT JOIN tblservices as c ON a.services_id = c.services_id 
         LEFT JOIN tbldebtur as d ON a.bill_to = d.CUCODE 
         LEFT JOIN tbldebtur as e ON b.debtur = e.CUCODE 
       ";

$SQL .= " WHERE a.curr like '%".$curr."%' AND a.bill_nd = 'set' AND (a.nd_ever_created_status = '' OR a.nd_ever_created_status IS NULL) ";

$SQL .= " ORDER BY b.jo_code DESC ";

//echo $SQL;

          $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
          return $MyResult;
}


class PDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT,$curr;
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }
        $this->SetFont('arialn','',12);
                $this->Cell(195,5,$perush,0,1,'C',0);
                $this->Cell(195,5,$title,0,1,'C',0);
                $SystemDate = date("d/m/Y");
                $this->Cell(195,5,"REPORT DATE: ".$SystemDate,0,1,'C',0);
                $this->Cell(195,5,"CURRENCY: ".$curr,0,1,'C',0);
                $this->Cell(195,5,$page." ".$this->PageNo(),0,1,'R',0);
                $this->Line(10,30,205,30);
                $this->Ln();
                $this->Ln();
                //Header

                //Column widths
                $w=array(30,30,50,30,50,50);
                
                $this->SetFont('arialn','',8);

                $this->Cell($w[0],7,"JOB ORDER NO.",1,0,'C',0);
                $this->Cell($w[1],7,"DATE",1,0,'C',0);
                $this->Cell($w[2],7,"SERVICES",1,0,'C',0);
                $this->Cell($w[3],7,"AMOUNT",1,0,'C',0);
                $this->Cell($w[4],7,"CUSTOMER",1,0,'C',0);
                $this->Cell($w[5],7,"BILL TO",1,0,'C',0);
                $this->Ln();
    }

    function ReportTable($Result) {
		global $curr;
        //Column widths
        $w=array(30,30,50,30,50,50);
        
        $TotalAmount = 0;
        
        //Data
        foreach($Result as $row)
        {

                $no++;
                $this->Cell($w[0],6,$row["jo_code"],'LR',0,'C');
                
                list($year,$month,$day) = explode("-",$row["jo_date"]);
                $FDate = "$day-$month-$year";                
                                
                $this->Cell($w[1],6,$FDate,'LR',0,'C');
                $this->Cell($w[2],6,$row["services_name"],'LR');
                
                if($curr == "USD")
                {
                  $this->Cell($w[3],6,number_format($row["total_amount_usd"],2),'LR',0,'R');
                  $TotalAmount+= $row["total_amount_usd"];
                }
                
                else if($curr == "IDR")
                {
                  $this->Cell($w[3],6,number_format($row["total_amount"],1),'LR',0,'R');
                  $TotalAmount+= $row["total_amount"];
                }
                
                $this->Cell($w[4],6,$row["cust_debtur"],'LR');
                $this->Cell($w[5],6,$row["bill_debtur"],'LR');
                
                                 
            $this->Ln();
        }


            $this->Cell($w[0]+$w[1]+$w[2],6,"TOTAL:",'LTR');
            $this->Cell($w[3],6,number_format($TotalAmount,2),'LTR',0,'R');
            $this->Cell($w[4]+$w[5],6,"",'LTR',0,'R');
            $this->Ln();

            
        //Closure line
        //$this->Cell(array_sum($w),0,'','T');
    }

    function Footer() {
        //Closure line
        $this->Cell(240,0,'','T');
    }

}


?>
